// 手机号码效验
export function validateVETel(value) {
  // const reg = /^1[3456789]\d{9}$/
  const reg = /^((\d{3,4}-\d{7,8})|(1[3|4|5|6|7|8|9][0-9]\d{8}))$/
  return !reg.test(value)
}

// 保留两位小数
export function validateVENumber(value) {
  const reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
  return !reg.test(value)
}

// 正整数
export function validateVENum(value) {
  const reg = /^[0-9]*[1-9][0-9]*$/
  return !reg.test(value)
}

// 效验身份证
export function validateVEIdCard(value) {
  const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  return !reg.test(value)
}

// 递归三维数组
export function recursive(arr, id, ids, names) {
  const oldObj = {
    idList: [],
    nameList: []
  }
  for (let i = 0; i < arr.length; i++) {
    if (id === arr[i][ids]) {
      oldObj.idList.push(arr[i][ids])
      oldObj.nameList.push(arr[i][names])
      return oldObj
    } else if (arr[i].children && arr[i].children.length > 0) {
      const newObj = recursive(arr[i].children, id, ids, names)
      if (newObj && newObj.idList.length > 0) {
        oldObj.idList.push(arr[i][ids])
        oldObj.nameList.push(arr[i][names])
        oldObj.idList = oldObj.idList.concat(newObj.idList)
        oldObj.nameList = oldObj.nameList.concat(newObj.nameList)
        return oldObj
      }
    }
  }
}

// 保留两位小数
export function isInteger(value) {
  if (!value) return '0.00'
  var values = value.toFixed(2)
  var value2Array = values.split('.')
  var intPart = value2Array[0] // 获取整数部分
  var intPartFormat = intPart
    .toString()
    .replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断

  return intPartFormat + '.' + value2Array[1]
}

export function passsWord(value) {
  const reg = /^[a-zA-Z0-9]{6,12}$/
  if (!reg.test(value)) {
    return true
  } else {
    return false
  }
}

//递归营销区域
export function marketingArea(ids, arr) {
  var obj = undefined
  for (let i = 0; i < arr.length; i++) {
    var regionList = arrReduce(arr[i].regionList)
    if (regionList.findIndex(a => a.regionCode === ids) !== -1) {
      if (arr[i].children && arr[i].children.length > 0) {
        var newObj = marketingArea(ids, arr[i].children)
        if (newObj) {
          obj = newObj
        } else {
          obj = arr[i]
        }
      } else {
        obj = arr[i]
      }
    }
  }
  return obj
}



function arrReduce(someArr) {
  var arrMax = []
  for (let i = 0; i < someArr.length; i++) {
    if (Array.isArray(someArr[i].children)) {
      arrMax = arrMax.concat(arrReduce(someArr[i].children))
    } else {
      arrMax.push(someArr[i]);
    }
  }
  return arrMax
}

